home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / nshellmegasource1.50 / mega src / lib / buf_utl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-26  |  1.9 KB  |  103 lines  |  [TEXT/KAHL]

  1. /* ========================================
  2.  
  3.     buf_utl.c
  4.     
  5.     These routines allow buffered output in nShell(tm)
  6.     commands.  To use this module, you must:
  7.     
  8.         1. Set up the A4 register for global storage.
  9.         
  10.         2. Call buf_init at the start of your command.
  11.         
  12.         3. Call buf_flush at the end of your command.
  13.     
  14.     Copyright (c) 1994 Newport Software Development
  15.     
  16.     You may distribute unmodified copies of this file for
  17.     noncommercial purposes.  You may use this file as a
  18.     reference when writing your own programs.
  19.     
  20.     All other rights are reserved.
  21.     
  22.    ======================================== */
  23.    
  24. #define BUF_SIZE    2048
  25.  
  26. #include "nshc.h"
  27.  
  28. #include "buf_utl.proto.h"
  29.  
  30. /* ======================================== */
  31.  
  32. // globals
  33.  
  34. int                 g_buf_ptr;            // next char to use in output buffer
  35. char             g_buf[BUF_SIZE];    // output buffer
  36. t_nshc_calls    *g_buf_calls;        // global copy of nshc_calls to reduce parm passing
  37.  
  38. /* ======================================== */
  39.  
  40. void buf_init( t_nshc_calls *nshc_calls )
  41. {
  42.     g_buf_ptr = 0;
  43.     g_buf_calls = nshc_calls;
  44. }
  45.  
  46. /* ======================================== */
  47.  
  48. void buf_flush( void )
  49. {
  50.     if (g_buf_ptr > 0) {
  51.         g_buf[g_buf_ptr++] = 0;
  52.         g_buf_calls->NSH_puts(g_buf);
  53.     }
  54.     g_buf_ptr = 0;
  55. }
  56.  
  57. /* ======================================== */
  58.  
  59. void buf_putchar( char ch )
  60. {
  61.     if (g_buf_ptr >= BUF_SIZE-1)
  62.         buf_flush();
  63.     g_buf[g_buf_ptr++] = ch;
  64. }
  65.  
  66. /* ======================================== */
  67.  
  68. void buf_puts( char *p )
  69. {
  70.     char ch;
  71.     
  72.     while ( ch = *p++ ) {
  73.     
  74.         if (g_buf_ptr >= BUF_SIZE-1)
  75.             buf_flush();
  76.             
  77.         g_buf[g_buf_ptr++] = ch;
  78.     
  79.         }
  80. }
  81.  
  82. /* ======================================== */
  83.  
  84. void buf_putStr( StringPtr str )
  85. {
  86.     int     i;
  87.     char    *from;
  88.     char    *to;
  89.  
  90.     if (g_buf_ptr + str[0] >= BUF_SIZE-1)
  91.         buf_flush();
  92.         
  93.     from = (char *)&str[1];
  94.     to = &(g_buf[g_buf_ptr]);
  95.     
  96.     for (i = str[0]; i > 0; i--)
  97.         *to++ = *from++;
  98.         
  99.     g_buf_ptr += str[0];
  100. }
  101.  
  102. /* ======================================== */
  103.